package com.jketing.controller.spring.mvc.converter;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jketing.entity.ams.work.Variety;

/**
 * StringToVarietiesConverter 自定义类型转化器
 * 
 * @author  李荣华
 * @version v1.0
 * @date    2018-10-16
 * @since   Spring-mvc 4.3.8
 */
@Component
public class StringToVarietiesConverter implements Converter<String, List<Variety>> {

	@Override
	public List<Variety> convert(String source) {
		List<Variety> list = null;
		if(StringUtils.isNotBlank(source)){
			ObjectMapper mapper = new ObjectMapper();
			JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class,Variety.class);
			try {
				list = mapper.readValue(source,javaType);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}


